package com.ytjj.qmyx.data.amqp.consumer;

import com.alibaba.fastjson.JSONArray;
import com.rabbitmq.client.Channel;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.qmyx.data.model.MengWangRequestVo;
import com.ytjj.qmyx.data.model.MengWangResponse;
import com.ytjj.qmyx.data.model.SanTongRequestVo;
import com.ytjj.qmyx.data.model.XinycSmsRequest;
import com.ytjj.qmyx.data.service.MsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *  *
 **/
@Slf4j
@Component
public class MsgLogListener {

    @Resource
    private MsgService msgService;


    @RabbitListener(queuesToDeclare = @Queue(value = QueueConstants.MSG_CALLBACK_FOR_XYC, durable = "true"))
    public void updateXinycMsgInfo(String xinyichen, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        log.info("====收到一条xinyichen回调请求的短信消息{}====", xinyichen);
        List<XinycSmsRequest> xinycSmsRequests = JSONArray.parseArray(xinyichen, XinycSmsRequest.class);
        msgService.updateXinycMsgInfo(xinycSmsRequests);
    }

    @RabbitListener(queuesToDeclare = @Queue(value = QueueConstants.MSG_CALLBACK_FOR_SANTONG, durable = "true"))
    public void updateSanTongMsgInfo(String santong, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        log.info("===收到一条santong回调请求的短信消息{}===", santong);
        List<SanTongRequestVo> sanTongRequestVos = JSONArray.parseArray(santong, SanTongRequestVo.class);
        msgService.updateSanTongMsgInfo(sanTongRequestVos);
    }

    @RabbitListener(queuesToDeclare = @Queue(value = QueueConstants.MSG_CALLBACK_FOR_MENGWANG, durable = "true"))
    public void updateMengWangMsgInfo(String mengwang, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        log.info("===收到一条mengwang回调请求的短信消息{}===", mengwang);
        List<MengWangRequestVo> mengWangRequestVos = JSONArray.parseArray(mengwang, MengWangRequestVo.class);
        msgService.updateMengWangMsgInfo(mengWangRequestVos);
    }
}
